﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using SeDuce.IDAL;
using System.Data.SqlClient;

namespace SeDuce.DAL
{
    public class SizeDAL : ISizeDAL
    {
        #region SQL
        public const string sqlGetSizes = "SELECT * FROM TbSize";
        const string sqlUpdateSize = "UPDATE TbSize SET SizeName = {0},Sort = {1} WHERE (SizeID = {2})";
        const string sqlInsertSize = "INSERT INTO TbSize (SizeName, Sort) VALUES ({0},{1})";
        const string sqlDeleteSize = "Delete from TbSize where SizeID={0}";
        #endregion

        #region Variables
        private static DataAccessObjectBase _dao = null;
        public static DataAccessObjectBase DAO
        {
            get
            {
                if (_dao == null)
                    _dao = new DataAccessObjectBase();
                return _dao;
            }
            set
            {
                _dao = value;
            }
        }
        #endregion

        public System.Data.DataTable GetSizes()
        {
            return DAO.Select(sqlGetSizes);
        }

        public bool UpdateSize(Model.Size size)
        {
            SqlParameter[] sp = new SqlParameter[] {
                new SqlParameter ("@SizeName", size.SizeName),
                new SqlParameter ("@Sort", size.Sort),
                new SqlParameter ("@SizeID", size.SizeID)
            };
            string sql = string.Format(sqlUpdateSize, "@SizeName", "@Sort", "@SizeID");

            return DAO.ExecuteSql(sql, sp);
        }

        public bool UpdateSizes(List<Model.Size> sizes)
        {
            Dictionary<SqlParameter[], string> updateSqls = new Dictionary<SqlParameter[], string>();
            for (int i = 0; i < sizes.Count; i++)
            {
                SqlParameter[] parms = new SqlParameter[] { 
                    new SqlParameter ("@SizeName",sizes[i].SizeName),
                    new SqlParameter ("@Sort",sizes[i].Sort),
                    new SqlParameter ("@SizeID",sizes[i].SizeID)
                  };
                string sql = string.Format(sqlUpdateSize, "@SizeName", "@Sort", "@SizeID");
                updateSqls.Add(parms, sql);
            }
            return DAO.ExecuteSqlTran(updateSqls);
        }

        public bool InsertSize(Model.Size size)
        {
            SqlParameter[] sp = new SqlParameter[] {
                new SqlParameter ("@SizeName", size.SizeName),
                new SqlParameter ("@Sort", size.Sort)
            };
            string sql = string.Format(sqlInsertSize, "@SizeName", "@Sort");

            return DAO.ExecuteSql(sql, sp);
        }

        public bool DeleteSize(int sizeID)
        {
            SqlParameter[] sp = new SqlParameter[] { 
                new SqlParameter ("@SizeID",sizeID),
    
            };
            string sql = string.Format(sqlDeleteSize, "@SizeID");
            return DAO.ExecuteSql(sql, sp);
        }
    }
}
